package cn.com.homeyoda.aimate.aihealth.ui.yasee;

import android.app.Application;

import androidx.annotation.NonNull;

import com.wzq.mvvmsmart.event.StateLiveData;
import com.wzq.mvvmsmart.http.ResponseThrowable;
import com.wzq.mvvmsmart.utils.KLog;
import com.wzq.mvvmsmart.utils.RxUtils;
import com.wzq.mvvmsmart.utils.ToastUtils;

import cn.com.homeyoda.aimate.aihealth.base.BaseViewModel;
import cn.com.homeyoda.aimate.aihealth.data.ExaminationDetail;
import cn.com.homeyoda.aimate.aihealth.data.Repository;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class YaseeCheckResultViewModel extends BaseViewModel<Repository> {

    public StateLiveData<ExaminationDetail.DataBean> examinationDetailData;

    public YaseeCheckResultViewModel(@NonNull Application application, Repository model) {
        super(application, model);
        examinationDetailData = new StateLiveData<>();
    }

    public void fetchExaminationDetail(String id) {
        model.fetchExaminationDetail(id)
                .compose(RxUtils.observableToMain())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(YaseeCheckResultViewModel.this)
                .subscribe(new Observer<ExaminationDetail>() {
                    @Override
                    public void onSubscribe(Disposable d) {
//                        stateIsShowData.postLoading();
                    }

                    @Override
                    public void onNext(ExaminationDetail examinationDetail) {
                        if (examinationDetail.getCode() == 200) {
                            examinationDetailData.postValueAndSuccess(examinationDetail.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("进入onError" + e.getMessage());
                        // 关闭对话框
                        examinationDetailData.postError();
                        if (e instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) e).message);
                        }
                    }

                    @Override
                    public void onComplete() {
                        KLog.e("进入onComplete");
                        // 请求刷新完成收回
                        examinationDetailData.postIdle();
                    }
                });
    }
}
